package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.DwaConvertCode;
import cn.com.connext.scrm.domain.master.PointManager;
import cn.com.connext.scrm.service.PointManagerService;
import cn.com.connext.scrm.service.dto.DwaConvertCodeModel;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.utils.EasyExcelUtil;
import cn.com.connext.scrm.web.rest.vm.IdVM;
import cn.com.connext.scrm.web.rest.vm.PointManagerPageVM;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 2 * @Author: shawn
 * 3 * @Date: 2020/8/11 4:05 下午
 * 4
 */
@RestController
@RequestMapping("/api/pointManager/")
public class PointManagerResource {

    private final Logger log = LoggerFactory.getLogger(PointManagerResource.class);

    private final String url = "page/user/pointsShop/index";

    @Autowired
    private PointManagerService pointManagerService;

    /**
     * 编辑积分管理
     *
     * @param dto
     * @return
     */
    @PostMapping("edit")
    public JsonResult edit(@RequestBody PointManager dto) {
        JsonResult result = new JsonResult();
        try {
            PointManager pointManager = pointManagerService.detail(dto.getId());
            if(dto.getStartTime() ==null){
                dto.setStartTime(pointManager.getStartTime());
            }
            if(dto.getEndTime() ==null){
                dto.setEndTime(pointManager.getEndTime());
            }
            if(StringUtils.isEmpty(dto.getPointShopName())){
                dto.setPointShopName(pointManager.getPointShopName());
            }
           if(StringUtils.isEmpty(dto.getIndexImg())){
               dto.setIndexImg(dto.getIndexImg());
           }
            if(dto.getIsEnable()==null){
                dto.setIsEnable(pointManager.getIsEnable());
            }
            pointManagerService.edit(dto);
            result.setState(0);
            result.setMessage("成功");
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setMessage("系统繁忙");
            result.setState(-1);
        }
        return result;
    }

    /**
     * 新增积分管理
     *
     * @param dto
     * @return
     */
    @PostMapping("add")
    public JsonResult add(@RequestBody PointManager dto) {
        JsonResult result = new JsonResult();
        try {
            dto.setCreateTime(ZonedDateTime.now());
            dto.setIsEnable(1);
            pointManagerService.save(dto);
            dto.setPointShopUrl(url+"?id="+dto.getId());
            pointManagerService.edit(dto);
            result.setData(dto);
            result.setState(0);
            result.setMessage("成功");
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setMessage("系统繁忙");
            result.setState(-1);
        }
        return result;
    }

    @PostMapping("pagelist")
    public JsonResult Pagelist(@RequestBody PointManagerPageVM vm) {
        JsonResult result = new JsonResult();
        Page<PointManager> list = pointManagerService.list(vm);
        result.setData(list);
        result.setState(0);
        return result;
    }

    @PostMapping("list")
    public JsonResult list() {
        JsonResult result = new JsonResult();
        List<PointManager> list = pointManagerService.findAll();
        result.setData(list);
        result.setState(0);
        return result;
    }

    @PostMapping("detail")
    public JsonResult detail(@RequestBody IdVM vm) {
        JsonResult result = new JsonResult();
        try {
            PointManager pointManager = pointManagerService.detail(vm.getId());
            result.setState(0);
            result.setData(pointManager);
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setState(-1);
            result.setMessage("系统繁忙");
        }
        return result;
    }

    @PostMapping("deletePrizeGood")
    public JsonResult delete(@RequestBody IdVM vm) {
        JsonResult result = new JsonResult();
        try {
            pointManagerService.delete(vm.getId());
            result.setState(0);
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setState(-1);
            result.setMessage("系统繁忙");
        }
        return result;
    }

    @PostMapping("enable")
    public JsonResult enable(@RequestBody IdVM vm) {
        JsonResult result = new JsonResult();
        try {
            pointManagerService.enable(vm.getId());
            result.setState(0);
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setState(-1);
            result.setMessage("系统繁忙");
        }
        return result;
    }

    @PostMapping("/excelImport")
    public JsonResult excelImport(MultipartFile[] files){
        JsonResult result = new JsonResult();
        List<DwaConvertCode>  dwaList= new ArrayList<>();

        try {
            if(files != null && files.length > 0) {
                MultipartFile file = files[0];
                List<Object> list = EasyExcelUtil.readExcel(file, new DwaConvertCodeModel(), 1, 1);
                if (list != null && list.size() > 0) {
                    DwaConvertCode dwaCode = null;
                    for (Object o : list) {
                        dwaCode =new DwaConvertCode();
                        DwaConvertCodeModel code = (DwaConvertCodeModel) o;
                        dwaCode.setCode(code.getCode());
                        dwaCode.setIsUse(1);
                        dwaList.add(dwaCode);
                    }
                    pointManagerService.addAllDwaCode(dwaList);
    //                System.out.println(modelList.size());
                    result.setState(0);
                    result.setMessage("导入完成");
                }
            }
        } catch (Exception e) {
            log.error("e:{}", e);
            result.setState(-1);
            result.setMessage("系统繁忙");
        }
            return result;
    }
}
